package x10.utilities;

/**
 *  Utils - General purpose (ie catch-all) utility class
 *
 *  @author Denny Sheirer
 *  @date 20110120
 */
public class Utils {

    /**
     * Utility method to reverse (end-for-end) the bits of a byte
     * @param fwdByte - byte to be reversed
     * @return - reversed byte
     */
    public static byte reverseBits(byte fwdByte){
        return (byte)(Integer.reverse(fwdByte)>>24);
    }

    /**
     *  Utility method to convert a byte to a hex string
     * @param bits - byte to be converted to hex string
     * @return - string with hexadecimal representation of byte
     */
    public static String toHexString(byte bits){
        return String.format("%1$02X", bits);
    }

    /**
     *  Utility method to join two bytes and return hex (4-digit) string
     * @param byte1 - upper byte
     * @param byte2 - lower byte
     * @return - hex string representing joined byte value
     */
    public static String toHexString(byte byte1, byte byte2){
        String str1 = String.format("%1$02X", byte1);
        String str2 = String.format("%1$02X", byte2);

        return "0x" + str1 + str2;
    }

    /**
     * Utility method to convert a byte array to hex string
     * @param bytes - byte array
     * @param byteCount - count of bytes in byte array
     * @return - hex string representation
     */
    public static String byteArrayToString(byte[] bytes, int byteCount){
        StringBuilder sb = new StringBuilder();

        for (int i = 0; i < byteCount; i++) {
            sb.append("0x");
            sb.append(String.format("%1$02X", bytes[i]));
            sb.append(" ");
        }
        return sb.toString();
    }

    /**
     * Utility method to convert two bytes to an integer
     * @param high byte
     * @param low byte
     * @return - integer value of joined bytes
     */
    public static int getIntFromTwoBytes(byte high, byte low){
        int returnVal = 0;

        returnVal |= high & 0xFF;
        returnVal <<= 8;
        returnVal |= low & 0xFF;

        return returnVal;
    }

    /**
     * Utility method to return a signed short from two bytes
     * @param high byte
     * @param low byte
     * @return - short value of joined bytes
     */
    public static short getSignedShortFromTwoBytes(byte high, byte low){
        short returnVal = 0;

        returnVal |= high & 0xFF;
        returnVal <<= 8;
        returnVal |= low & 0xFF;

        return returnVal;
    }

    /**
     * Utility to merge two ChunkOfBytes objects
     * @param b1 ChunkOfBytes1
     * @param b2 ChunkOfBytes2
     * @return ChunkOfBytes merged
     */
    public static ChunkOfBytes mergeBytes(ChunkOfBytes b1, ChunkOfBytes b2){
        ChunkOfBytes br = new ChunkOfBytes();
        br.setLength(b1.getLength() + b2.getLength());

        byte[] mergedBytes = new byte[br.getLength()];

        System.arraycopy(b1.getBytes(), 0, mergedBytes, 0, b1.getLength());
        System.arraycopy(b2.getBytes(), 0, mergedBytes, b1.getLength(), b2.getLength());

        br.setBytes(mergedBytes);
        br.setEmpty(false);

        return br;
    }

}
